/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.crawler.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.crawler.entity.CrawlerTheme;
import com.thinkgem.jeesite.modules.crawler.service.CrawlerThemeService;

/**
 * 主题模块Controller
 * @author 	mj
 * @version 2017-12-25
 */
@Controller
@RequestMapping(value = "${adminPath}/crawlerweb/crawlerTheme")
public class CrawlerThemeController extends BaseController {

	@Autowired
	private CrawlerThemeService crawlerThemeService;
	
	
	@ModelAttribute
	public CrawlerTheme get(@RequestParam(required=false) String id) {
		CrawlerTheme entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = crawlerThemeService.get(id);
		}
		if (entity == null){
			entity = new CrawlerTheme();
		}
		return entity;
	}
	
	@RequiresPermissions("crawlerweb:crawlerTheme:view")
	@RequestMapping(value = {"list", ""})
	public String list(CrawlerTheme crawlerTheme, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<CrawlerTheme> page = crawlerThemeService.findPage(new Page<CrawlerTheme>(request, response), crawlerTheme); 
		model.addAttribute("page", page);
		return "modules/crawlerweb/crawlerThemeList";
	}

	@RequiresPermissions("crawlerweb:crawlerTheme:view")
	@RequestMapping(value = "form")
	public String form(CrawlerTheme crawlerTheme, Model model) {
		model.addAttribute("crawlerTheme", crawlerTheme);
		return "modules/crawlerweb/crawlerThemeForm";
	}

	@RequiresPermissions("crawlerweb:crawlerTheme:edit")
	@RequestMapping(value = "save")
	public String save(CrawlerTheme crawlerTheme, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, crawlerTheme)){
			return form(crawlerTheme, model);
		}
		crawlerThemeService.save(crawlerTheme);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:"+Global.getAdminPath()+"/crawlerweb/crawlerTheme/?repage";
	}
	
	@RequiresPermissions("crawlerweb:crawlerTheme:edit")
	@RequestMapping(value = "delete")
	public String delete(CrawlerTheme crawlerTheme, RedirectAttributes redirectAttributes) {
		crawlerThemeService.delete(crawlerTheme);
		addMessage(redirectAttributes, "删除开放数据集归类成功");
		return "redirect:"+Global.getAdminPath()+"/crawlerweb/crawlerTheme/?repage";
	}
	

	/**
	 * 以树的形式返回归类数据
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(HttpServletResponse response) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		//获取所有归类
		List<CrawlerTheme> list = crawlerThemeService.findList(new CrawlerTheme());
		//伪造根节点
		CrawlerTheme root = new CrawlerTheme();
		root.setId("");
		root.setName("根节点");
		list.add(root);	
		for (int i=0; i<list.size(); i++){
			CrawlerTheme e = list.get(i);
				Map<String, Object> map = Maps.newHashMap();
				map.put("id", e.getId());
				map.put("pId", root.getId());
				map.put("pIds", "");
				map.put("name", e.getName());
				mapList.add(map);
		}
		return mapList;
	}
}